package com.newway.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.back.entity.NearbyPlaces;
import com.newway.back.mapper.NearbyPlacesMapper;
import com.newway.common.result.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/system/place")
public class NearbyPlacesController {

    @Resource
    NearbyPlacesMapper nearbyPlacesMapper;

    @GetMapping("/getPlaceList")
    public Result getList(NearbyPlaces nearbyPlaces){
        QueryWrapper<NearbyPlaces> nearbyPlacesQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(nearbyPlaces.getName())){
            nearbyPlacesQueryWrapper.like("name",nearbyPlaces.getName());
        }
        if(StringUtils.isNotBlank(nearbyPlaces.getStatus())){
            nearbyPlacesQueryWrapper.like("status",nearbyPlaces.getStatus());
        }
        Page<NearbyPlaces> nearbyPlacesPage = new Page<>(nearbyPlaces.getPageNum(),nearbyPlaces.getPageSize());
        Page<NearbyPlaces> page = nearbyPlacesMapper.selectPage(nearbyPlacesPage, nearbyPlacesQueryWrapper);
        return Result.success(page);
    }

    @PostMapping("/addPlace")
    public Result addPlace(@RequestBody NearbyPlaces nearbyPlaces){
        nearbyPlacesMapper.insert(nearbyPlaces);
        return Result.success();
    }

    @PostMapping("/updatePlace")
    public Result updatePlace(@RequestBody NearbyPlaces nearbyPlaces){
        nearbyPlacesMapper.updateById(nearbyPlaces);
        return Result.success();
    }

    @PostMapping("/delPalce/{id}")
    public Result delPlace(@PathVariable("id") Integer id){
        nearbyPlacesMapper.deleteById(id);
        return Result.success();
    }
}
